/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true,
  },
  {
    path: '/callback',
    component: () => import('@/views/callback'),
    hidden: true,
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true,
  },
]

export const asyncRoutes = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      title: '',
      icon: 'apps-2-line',
    },
    children: [
      // 首页
      {
        key: 0,
        path: '/',
        name: 'Index',
        component: () => import('@/views/member/users'),
        hidden: true,
        meta: {
          title: '首页',
          affix: true,
          icon: 'home-2-line',
          roles: ['Index'],
        },
      },
      // 用户管理
      {
        key: 1,
        path: '/member',
        name: 'Member',
        component: () => import('@/views/member'),
        redirect: 'members',
        meta: {
          title: '用户管理',
          icon: 'calendar-line', //home-2-line
          roles: ['Member'],
        },
        children: [
          {
            key: 11,
            path: '/userList',
            name: 'UserList',
            component: () => import('@/views/member/users'),
            meta: {
              title: 'APP用户列表',
              roles: ['UserList'],
            },
          },
          {
            key: 12,
            path: '/adminList',
            name: 'AdminList',
            component: () => import('@/views/member/adminList'),
            meta: {
              title: '税筹用户列表',
              roles: ['AdminList'],
            },
          },
          // {
          //   key: 13,
          //   path: '/superUser',
          //   name: 'SuperUser',
          //   component: () => import('@/views/member/super'),
          //   meta: {
          //     title: '设置大咖',
          //     roles: ['SuperUser'],
          //   },
          // },
          // {
          //   key: 14,
          //   path: '/companyAuth',
          //   name: 'CompanyAuth',
          //   component: () => import('@/views/member/companyAuth'),
          //   meta: {
          //     title: '企业认证',
          //     roles: ['CompanyAuth'],
          //   },
          // },
          // {
          //   key: 15,
          //   path: '/sysMessage',
          //   name: 'SysMessage',
          //   component: () => import('@/views/member/sysMessage'),
          //   meta: {
          //     title: '系统消息',
          //     roles: ['SysMessage'],
          //   },
          // },
          {
            key: 16,
            path: '/supplierList',
            name: 'SupplierList',
            component: () => import('@/views/member/supplierList'),
            meta: {
              title: '供应商总库',
              roles: ['SupplierList'],
            },
          },
        ],
      },

      // 消息管理
      {
        key: 17,
        path: '/message',
        name: 'Message',
        component: () => import('@/views/message'),
        redirect: 'message',
        meta: {
          title: '消息管理',
          icon: 'calendar-line', //home-2-line
          roles: ['Message'],
        },
        children: [
          // 税筹系统消息
          {
            key: 171,
            path: '/notice',
            name: 'Notice',
            component: () => import('@/views/message/notice'),
            meta: {
              title: '税筹系统消息',
              roles: ['Notice'],
            },
          },
          {
            key: 172,
            path: '/sysMessage',
            name: 'SysMessage',
            component: () => import('@/views/message/sysMessage'),
            meta: {
              title: 'APP系统消息',
              roles: ['SysMessage'],
            },
          },
          {
            key: 173,
            path: '/message',
            name: 'Message',
            component: () => import('@/views/message/message'),
            hidden: true,
            meta: {
              title: '消息合并',
              roles: ['Message'],
            },
          },
        ]
      },
      // 内容管理
      {
        key: 2,
        path: '/contents',
        name: 'Contents',
        component: () => import('@/views/contents'),
        redirect: 'contents',
        meta: {
          title: 'APP内容管理',
          icon: 'calendar-line', //home-2-line
          roles: ['Contents'],
        },
        children: [
          // 服务列表
          {
            key: 21,
            path: '/service',
            name: 'Service',
            component: () => import('@/views/contents/service'),
            meta: {
              title: '服务资源',
              roles: ['Service'],
            },
          },
          {
            key: 22,
            path: '/demand',
            name: 'Demand',
            component: () => import('@/views/contents/demand'),
            meta: {
              title: '需求资源',
              roles: ['Demand'],
            },
          },
          {
            key: 23,
            path: '/information',
            name: 'Information',
            component: () => import('@/views/contents/information'),
            meta: {
              title: '资讯管理',
              roles: ['Information'],
            },
          },
          {
            key: 24,
            path: '/works',
            name: 'Works',
            component: () => import('@/views/contents/works'),
            meta: {
              title: '作品管理',
              roles: ['Works'],
            },
          },
          {
            key: 25,
            path: '/recruit',
            name: 'Recruit',
            component: () => import('@/views/contents/recruit'),
            meta: {
              title: '招聘管理',
              roles: ['Recruit'],
            },
          },
          {
            key: 26,
            path: '/cooperation',
            name: 'Cooperation',
            component: () => import('@/views/contents/cooperation'),
            meta: {
              title: '合作伙伴',
              roles: ['Cooperation'],
            },
          },
          {
            key: 27,
            path: '/course',
            name: 'Course',
            component: () => import('@/views/contents/course'),
            meta: {
              title: '课程培训',
              roles: ['Course'],
            },
          },
          {
            key: 28,
            path: '/live',
            name: 'Live',
            component: () => import('@/views/contents/live'),
            meta: {
              title: '直播管理',
              roles: ['Live'],
            },
          },
          {
            key: 29,
            path: '/companyAuth',
            name: 'CompanyAuth',
            component: () => import('@/views/member/companyAuth'),
            meta: {
              title: '认证管理',
              roles: ['CompanyAuth'],
            },
          },
        ],
      },

      // 社交管理
      {
        key: 3,
        path: '/socials',
        name: 'Social',
        component: () => import('@/views/socials'),
        redirect: 'socials',
        meta: {
          title: 'APP社交管理',
          icon: 'bill-line',
          roles: ['Social'],
        },
        children: [
          // 圈子管理
          {
            key: 31,
            path: '/circles',
            name: 'Circles',
            component: () => import('@/views/socials/circle'),
            meta: {
              title: '圈子管理',
              roles: ['Circles'],
            },
          },
          // 群管理
          {
            key: 32,
            path: '/group',
            name: 'Group',
            component: () => import('@/views/socials/group'),
            // hidden: true,
            meta: {
              title: '群管理',
              roles: ['Group'],
            },
          },
        ],
      },

      // 税筹平台设置
      {
        key: 4,
        path: '/tax',
        name: 'Tax',
        component: () => import('@/views/tax'),
        redirect: 'tax',
        meta: {
          title: '税筹费率管理',
          icon: 'bill-line',
          roles: ['Tax'],
        },
        children: [
          // 费率
          {
            key: 41,
            path: '/rate',
            name: 'Rate',
            component: () => import('@/views/tax/rate'),
            meta: {
              title: '费率设置',
              roles: ['Rate'],
            },
          },
          // 大额
          {
            key: 42,
            path: '/identity',
            name: 'Identity',
            component: () => import('@/views/tax/identity'),
            // hidden: true,
            meta: {
              title: '大额设置',
              roles: ['Identity'],
            },
          },
        ],
      },

      // 收款人
      // {
      //   key: 5,
      //   path: '/personnel',
      //   name: 'Personnel',
      //   component: () => import('@/views/personnel'),
      //   redirect: 'personnel',
      //   meta: {
      //     title: '收款人管理',
      //     icon: 'bill-line',
      //     roles: ['Personnel'],
      //   },
      //   children: [
      //     // 供应商
      //     {
      //       key: 51,
      //       path: '/business',
      //       name: 'Business',
      //       component: () => import('@/views/personnel/business'),
      //       meta: {
      //         title: '供应商类别添加',
      //         roles: ['Business'],
      //       },
      //     },
      //     // 服务商
      //     {
      //       key: 52,
      //       path: '/company',
      //       name: 'Company',
      //       component: () => import('@/views/personnel/company'),
      //       // hidden: true,
      //       meta: {
      //         title: '服务商类别添加',
      //         roles: ['Company'],
      //       },
      //     },
      //   ],
      // },

      //付款
      {
        key: 6,
        path: '/payments',
        name: 'Payments',
        component: () => import('@/views/payment'),
        meta: {
          title: '税筹付款管理',
          icon: 'money-cny-box-line',
          roles: ['Payments'],
        },
        children: [
          // 付款管理
          {
            key: 61,
            path: '/payment',
            name: 'Payment',
            component: () => import('@/views/payment/payment'),
            meta: {
              title: '付款管理',
              roles: ['Payment'],
            },
          },
          // 收款明细
          {
            key: 62,
            path: '/collection',
            name: 'Collection',
            component: () => import('@/views/payment/collection'),
            meta: {
              title: '收款明细',
              roles: ['Collection'],
            },
          },
          // 供应商
          // {
          //   key: 53,
          //   path: '/business',
          //   name: 'Business',
          //   component: () => import('@/views/personnel/business'),
          //   meta: {
          //     title: '供应商类别添加',
          //     roles: ['Business'],
          //   },
          // },
          // // 服务商
          // {
          //   key: 54,
          //   path: '/company',
          //   name: 'Company',
          //   component: () => import('@/views/personnel/company'),
          //   // hidden: true,
          //   meta: {
          //     title: '服务商类别添加',
          //     roles: ['Company'],
          //   },
          // },
        ],
      },

      //充值
      {
        key: 7,
        path: '/recharge',
        name: 'Recharge',
        component: () => import('@/views/recharge/index'),
        meta: {
          title: '税筹充值管理',
          icon: 'bill-line',
          roles: ['Recharge'],
        },
      },

      // 优惠券
      {
        key: 8,
        path: '/coupon',
        name: 'Coupon',
        component: () => import('@/views/coupon/coupon'),
        meta: {
          title: '税筹优惠券管理',
          icon: 'bill-line',
          roles: ['Coupon'],
        },
      },

      // 开票管理
      {
        key: 9,
        path: '/invoice',
        name: 'Invoice',
        component: () => import('@/views/invoice/invoice'),
        meta: {
          title: '税筹开票管理',
          icon: 'bill-line',
          roles: ['Invoice'],
        },
      },

      // 渠道商管理
      {
        key: 10,
        path: '/supplier',
        name: 'Supplier',
        component: () => import('@/views/supplier'),
        redirect: 'supplier',
        meta: {
          title: '税筹渠道商管理',
          icon: 'bill-line',
          roles: ['Supplier'],
        },
        children: [
          // 列表
          {
            key: 101,
            path: '/list',
            name: 'List',
            component: () => import('@/views/supplier/list'),
            meta: {
              title: '渠道商列表',
              roles: ['List'],
            },
          },
          // 记录
          {
            key: 102,
            path: '/record',
            name: 'Record',
            component: () => import('@/views/supplier/record'),
            // hidden: true,
            meta: {
              title: '分润记录',
              roles: ['Record'],
            },
          },
        ],
      },

      // 系统设置
      {
        key: 11,
        path: '/power',
        name: 'Power',
        component: () => import('@/views/power'),
        meta: {
          title: '系统管理',
          icon: 'bill-line',
          roles: ['Power'],
        },
        children: [
          {
            key: 111,
            path: '/setAdmin',
            name: 'setAdmin',
            component: () => import('@/views/power/admin'),
            meta: {
              title: '用户管理',
              roles: ['setAdmin'],
            },
          },
          {
            key: 112,
            path: '/setting',
            name: 'Setting',
            component: () => import('@/views/power/setting'),
            meta: {
              title: 'APP系统设置',
              roles: ['Setting'],
            },
          },
          {
            key: 113,
            path: '/wordbook',
            name: 'Wordbook',
            component: () => import('@/views/power/wordbook'),
            hidden: true,
            meta: {
              title: '税筹数据字典',
              roles: ['Wordbook'],
            },
          },
          {
            key: 116,
            path: '/zhuanpiao',
            name: 'Zhuanpiao',
            component: () => import('@/views/power/zhuanpiao'),
            meta: {
              title: '专票类型设置',
              roles: ['Zhuanpiao'],
            },
          },
          {
            key: 114,
            path: '/journal',
            name: 'journal',
            component: () => import('@/views/power/journal'),
            meta: {
              title: '日志管理',
              roles: ['journal'],
            },
          },
          {
            key: 115,
            path: '/administrator',
            name: 'Administrator',
            component: () => import('@/views/power/administrator'),
            hidden: true,
            meta: {
              title: '管理员设置',
              roles: ['Administrator'],
            },
          },
        ],
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
  },
]

const router = createRouter()

export function resetRouter(routes = constantRoutes) {
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

export default router
